package com.example.brisk.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.brisk.entity.Book;
import com.example.brisk.service.IBookService;
import com.example.brisk.vo.R;
import com.example.brisk.vo.req.BookSaveReqVo;
import com.example.brisk.vo.req.BookUpadateReqVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * book接口：实现增删改查
 * 1.遵守rest接口规范，对于同一资源的不同操作，使用不同的请求模式，
 * 获取：GET
 * 新增：POST
 * 修改: PUT
 * 删除：DELETE
 * 2.业务下沉到service层，controller层不做业务处理。
 */
@Api(value = "book", tags = "book api")
@RestController
@RequestMapping("/api/book")
public class BookController {

    @Autowired
    private IBookService bookService;

    @ApiOperation(value = "获取详情", notes = "传入id")
    @GetMapping("/{id}")
    public R<Book> getBookById(@PathVariable Integer id){
        return bookService.getBookById(id);
    }

    @ApiOperation(value = "创建book", notes = "传入book")
    @PostMapping("")
    public R saveBook(@RequestBody BookSaveReqVo vo){
        return bookService.saveBook(vo);
    }

    @ApiOperation(value = "编辑book", notes = "传入book")
    @PutMapping("")
    public R updateBook(@RequestBody BookUpadateReqVo vo){
        return bookService.updateBook(vo);
    }

    @ApiOperation(value = "删除book", notes = "传入id")
    @DeleteMapping("/{id}")
    public R removeBook(@PathVariable Integer id){
        return bookService.removeBook(id);
    }

    @ApiOperation(value = "获取book列表", notes = "")
    @GetMapping("/list")
    public R<List<Book>> getBookList(){
        return bookService.getBookList();
    }

    @ApiOperation(value = "分页获取book列表", notes = "")
    @GetMapping("/list/page")
    public R<Page<Book>> getBookListByPage(Integer pageNo, Integer pageSize){
        return bookService.getBookListByPage(pageNo, pageSize);
    }
}
